package com.zuihai.app.mvp.presenter;

import android.app.Application;

import com.blankj.utilcode.util.LogUtils;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.widget.imageloader.ImageLoader;
import com.zuihai.app.mvp.contract.AdCommentContract;
import com.zuihai.app.mvp.model.entity.BaseMain;
import com.zuihai.app.mvp.model.entity.BaseMainClass;
import com.zuihai.app.mvp.model.entity.MyWalletData;
import com.zuihai.app.mvp.model.entity.ad.GuanggaoDetailData;
import com.zuihai.app.mvp.ui.utils.AccountHelper;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;


/**
 * 通过Template生成对应页面的MVP和Dagger代码,请注意输入框中输入的名字必须相同
 * 由于每个项目包结构都不一定相同,所以每生成一个文件需要自己导入import包名,可以在设置中设置自动导入包名
 * 请在对应包下按以下顺序生成对应代码,Contract->Model->Presenter->Activity->Module->Component
 * 因为生成Activity时,Module和Component还没生成,但是Activity中有它们的引用,所以会报错,但是不用理会
 * 继续将Module和Component生成完后,编译一下项目再回到Activity,按提示修改一个方法名即可
 * 如果想生成Fragment的相关文件,则将上面构建顺序中的Activity换为Fragment,并将Component中inject方法的参数改为此Fragment
 */


/**
 * Created by Panda_Program on 2018/7/21 0021.
 */

@FragmentScope
public class AdCommentPresenter extends BasePresenter<AdCommentContract.Model, AdCommentContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;

    @Inject
    public AdCommentPresenter(AdCommentContract.Model model, AdCommentContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void guanggaoDetail(String guanggaoId){
        mModel.guanggaoDetail(guanggaoId)
                .retryWhen(new RetryWithDelay(3, 2))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BaseMain<GuanggaoDetailData>>() {
                    @Override
                    public void onCompleted() {
                        LogUtils.e("onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.e(e.toString());
                    }

                    @Override
                    public void onNext(BaseMain<GuanggaoDetailData> dataBaseMain) {
                        LogUtils.e("database   "+dataBaseMain.getData().getDescription());

                        mRootView.fillScreenDetailContent(dataBaseMain.getData());
                    }
                });
    }

    public void requestWallet() {

        mModel.requestWallet(AccountHelper.getInstance().getUid())
                .retryWhen(new RetryWithDelay(3, 3))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())

                .map(baseJson -> {
                    if (baseJson.isSuccess()) {
                        return baseJson.getData();
                    }
                    return null;
                })
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mRootView.showLoading();
                    }
                })
                .doAfterTerminate(new Action0() {
                    @Override
                    public void call() {
                        mRootView.hideLoading();
                    }
                })
                .subscribe(new ErrorHandleSubscriber<MyWalletData>(mErrorHandler) {
                    @Override
                    public void onNext(MyWalletData myWalletData) {
                        mRootView.setMoney(myWalletData.getQianbao());
                    }
                });
    }

    public void requestAd(String guanggaoId){
        mModel.requestAd(guanggaoId)
                .retryWhen(new RetryWithDelay(3, 2))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BaseMainClass>() {
                    @Override
                    public void onCompleted() {
                        LogUtils.e("onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.e(e.toString());
                    }

                    @Override
                    public void onNext(BaseMainClass dataBaseMain) {
                        if (dataBaseMain.getCode().equals("0000")){
                            mRootView.fillContent();
                        }else {
                            mRootView.fillError();
                        }

                    }
                });
    }

}